



import pymysql
class DBTools(object):
    # 定义普通数据库工具类

    @staticmethod
    def create_conn():
        # 创建数据库连接
        conn = pymysql.connect(host="211.103.136.244", port=7061, user="student",
                               password="iHRM_student_2022", database="ihrm", charset="utf8")
        # 将数据库连接对象，返回给调用者
        return conn
    @staticmethod
    def query_one(sql):
        # 查询一条
        conn = None
        cursor = None
        res1 = None
        try:
            # 使用类名调用静态方法，调用创建链接
            conn = DBTools.create_conn()
            # 创建游标
            cursor = conn.cursor()
            # 执行参数传入的sql语句
            cursor.execute(sql)
            # 查询一条
            res1 = cursor.fetchone()
            # 返回查询结果给调用者

        except Exception as e:
            print('查询时，出现异常：',e)
        finally:
            cursor.close()
            # 关闭游标
            conn.close()
            # 关闭连接
            return res1
        # 返回查询结果给调用者

    @staticmethod
    def db_iud(sql):
        conn = None
        cursor = None
        try:
            conn = DBTools.create_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
            print('增删改sql执行成功')
        except Exception as e:
            print('增删改时出现异常',e)
            conn.rollback()
        finally:
            cursor.close()
            conn.close()

if __name__ == '__main__':
    res = DBTools.query_one("select * from bs_user where id = 1549560379976200192;")
    print('查询的结果为：', res)
    res1=DBTools.db_iud("insert into bs_user(id,mobile,username) values('1122334','13928779455','小花花');")
    print(res1)



    def query_many(self,size,sql):
        pass
    def db_iud(self,sql):
        pass